package com.sailorj.medical.domain.join_nh;

import java.sql.Connection;

import com.sailorj.medical.util.MyTime;
import com.sailorj.medical.util.TimeCompare;
import com.sailorj.medical.util.UniqueOrderGenerate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.sailorj.medical.dao.FamilyDao;
import com.sailorj.medical.dao.Family_memberDAO;
import com.sailorj.medical.dao.PayBaseDao;
import com.sailorj.medical.database.DbUtil;
import com.sailorj.medical.domain.area.Area;
import com.sailorj.medical.domain.family.Family;
import com.sailorj.medical.domain.family_member.Family_member;
import com.sailorj.medical.exception.DbException;

public class Join_nh {
	private static final Logger LOG = LogManager.getLogger(Join_nh.class);
	private String join_nh_code;
	private String family_id;
	private String join_id_card;
	private Timestamp join_time;
	private String join_invoice_code;// 参合发票编号
	private String money;
	private Area area;
	private String user_id;
	private String join_year;
	private Family family;

	public Join_nh() {
		super();
	}

	/**
	 * 
	 * @Title: IsTime @Description: 判断是否在参合时间内 @return @throws
	 */
	public boolean IsTime() {
		PayBaseDao PayBaseDao = new PayBaseDao();

		Timestamp startTime;
		Timestamp endTime;
		boolean status = false;
		try {
			startTime = PayBaseDao.findByYear().get(0).getStartTime();
			endTime = PayBaseDao.findByYear().get(0).getEndTime();
			status = TimeCompare.isEffectiveDate(MyTime.getNowForTimestamp(), startTime, endTime);

		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return status;

	}

	Family_member family_member = new Family_member();

	public void add(String join_nh_code, HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Family_member family_member  = new Family_member(join_nh_code);
		Family_memberDAO familyDao = new Family_memberDAO();
		FamilyDao familydao = new FamilyDao();
		// 获得orderId的值
		UniqueOrderGenerate idWorker = new UniqueOrderGenerate(0, 0);
		Long OrderID = idWorker.nextId();// 设置order_id的值
		PayBaseDao payBaseDao = new PayBaseDao();
		Join_nh join_nh = new Join_nh();
		Connection conn=null;
		String money = payBaseDao.findByYear().get(0).getMoney();
		List<Family_member> familyList = familyDao.findFamilyByJoin_nh_code(join_nh_code);// 根据家庭成员ID查询出家庭成员信息
		String fimalycode = familyList.get(0).getFamilycode();
		String name = familyList.get(0).getMembername();
		String IDcard = familyList.get(0).getIDcard();
		join_nh.setFamily_id(fimalycode);
		join_nh.setJoin_id_card(IDcard);
		try{
			this.join_nh_code = join_nh_code + MyTime.getUserDate("yyyyMMddhhmmss");// 参合登记编号,农合证号+原始时间
			this.family_id = family_member.getFamilycode();// 家庭编号
			this.join_id_card = family_member.getIDcard();// 身份证号码
			this.join_invoice_code = String.valueOf(OrderID);// 设置参合发票编号
			// this.money = String.valueOf(money * i);
			this.money = money;
			this.user_id = session.getAttribute("userid").toString();
			this.join_year = MyTime.getStringNowYear();
			this.join_time = MyTime.getNowForTimestamp();
			this.area = familydao.findByKeyValue("familycode", fimalycode).get(0).getArea();
			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			family_member.editYear(join_nh_code);
			conn.commit();
		}catch (Exception e) {
			if(conn!=null){
				conn.rollback();
			}
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	/**
	 * 
	 * @Title: addInst @Description: 添加  @param area @param
	 *         agencode @param agenname @param grade @throws Exception @throws
	 *         family.setCreatetime(MyTime.getNowForTimestamp());
	 */
	public void addJoin_nh(String list, HttpServletRequest request) throws Exception {
		Connection conn = null;
		try {

			HttpSession session = request.getSession();
			List<Join_nh> Join_nh_list = new ArrayList<Join_nh>();
			Family_memberDAO familyDao = new Family_memberDAO();
			Join_nh join_nh = new Join_nh();
			PayBaseDao payBaseDao = new PayBaseDao();
			FamilyDao familydao = new FamilyDao();

			String join_nh_code = null;
			// 获得orderId的值
			UniqueOrderGenerate idWorker = new UniqueOrderGenerate(0, 0);
			Long OrderID = idWorker.nextId();// 设置order_id的值
			int i;
			String money = payBaseDao.findByYear().get(0).getMoney();

			String[] sourceStrArray = list.split(",");
			for (i = 0; i < sourceStrArray.length; i++) {// 取出庭成员ID
				join_nh_code = sourceStrArray[i];
				List<Family_member> familyList = familyDao.findFamilyByJoin_nh_code(join_nh_code);// 根据家庭成员ID查询出家庭成员信息
				String fimalycode = familyList.get(0).getFamilycode();
				String name = familyList.get(0).getMembername();
				String IDcard = familyList.get(0).getIDcard();
				join_nh.setFamily_id(fimalycode);
				join_nh.setJoin_id_card(IDcard);

				Join_nh_list.add(join_nh);// 放入数组
				LOG.info("Join_nh_list==" + Join_nh_list + "familyID==" + join_nh_code);

				// 初始化属性
				// this.area = areacode;
				this.join_nh_code = join_nh_code + MyTime.getUserDate("yyyyMMddhhmmss");// 参合登记编号,农合证号+原始时间
				this.family_id = fimalycode;// 家庭编号
				this.join_id_card = IDcard;// 身份证号码
				this.join_invoice_code = String.valueOf(OrderID);// 设置参合发票编号
				// this.money = String.valueOf(money * i);
				this.money = money;
				this.user_id = session.getAttribute("userid").toString();
				this.join_year = MyTime.getStringNowYear();
				this.join_time = MyTime.getNowForTimestamp();
				// this.area = familydao.findFamilyListByFimalyID(fimalycode).get(0).getArea();
				this.area = familydao.findByKeyValue("familycode", fimalycode).get(0).getArea();
				// 保存导数据库
				conn = DbUtil.getConn();
				conn.setAutoCommit(false);
				// 调用插入数据库私有方法
				this.saveToDB(conn);
				family_member.editYear(join_nh_code);
				conn.commit();
			}
			session.setAttribute("join_invoice_code", this.join_invoice_code);
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	/**
	 * 插入记录
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	private void saveToDB(Connection connection) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append(
					"insert into t_join_nh(areacode,join_nh_code,join_id_card,join_invoice_code,money,user_id,join_year,family_id,join_time)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = connection.prepareStatement(sql);
			// 依次设置动态参数的值，注意顺序
			int index = 1;
			pstmt.setString(index++, (this.area.getAreacode()).toString());// 行政区域编码
			pstmt.setString(index++, this.join_nh_code);
			pstmt.setString(index++, this.join_id_card);
			pstmt.setString(index++, this.join_invoice_code);

			pstmt.setString(index++, this.money);// 行政区域编码
			pstmt.setString(index++, this.user_id);
			pstmt.setString(index++, this.join_year);
			pstmt.setString(index++, this.family_id);
			pstmt.setTimestamp(index++, this.join_time);

			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}

	}

	public Join_nh(String join_nh_code, String family_id, String join_id_card, Timestamp join_time,
			String join_invoice_code, String money, Area area, String user_id, String join_year, Family family) {
		super();
		this.join_nh_code = join_nh_code;
		this.family_id = family_id;
		this.join_id_card = join_id_card;
		this.join_time = join_time;
		this.join_invoice_code = join_invoice_code;
		this.money = money;
		this.area = area;
		this.user_id = user_id;
		this.join_year = join_year;
		this.family = family;
	}

	public String getJoin_nh_code() {
		return join_nh_code;
	}

	public void setJoin_nh_code(String join_nh_code) {
		this.join_nh_code = join_nh_code;
	}

	public String getFamily_id() {
		return family_id;
	}

	public void setFamily_id(String family_id) {
		this.family_id = family_id;
	}

	public String getJoin_id_card() {
		return join_id_card;
	}

	public void setJoin_id_card(String join_id_card) {
		this.join_id_card = join_id_card;
	}

	public Timestamp getJoin_time() {
		return join_time;
	}

	public void setJoin_time(Timestamp join_time) {
		this.join_time = join_time;
	}

	public String getJoin_invoice_code() {
		return join_invoice_code;
	}

	public void setJoin_invoice_code(String join_invoice_code) {
		this.join_invoice_code = join_invoice_code;
	}

	public String getMoney() {
		return money;
	}

	public void setMoney(String money) {
		this.money = money;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getJoin_year() {
		return join_year;
	}

	public void setJoin_year(String join_year) {
		this.join_year = join_year;
	}

	public Family getFamily() {
		return family;
	}

	public void setFamily(Family family) {
		this.family = family;
	}

}
